/*
 * @Author: 兑震鹏
 * @Description: 
 * @Date: 2020-05-26 18:51:19
 * @LastEditTime: 2020-05-27 16:07:19
 */
import * as cesium from 'cesium/Cesium'
const Cesium =cesium
export default class PolylineTrailLinkMaterial {
  constructor(cesium) {
    this.cesium = cesium;
    this.install();
  }
  install() {
    // const Cesium = this.cesium;
    /*
        流动纹理线
         color 颜色
         duration 持续时间 毫秒
      */
    function PolylineTrailLinkMaterialProperty(color, duration) {
      this._definitionChanged = new Cesium.Event();
      this._color = undefined;
      this._colorSubscription = undefined;
      this.color = color;
      this.duration = duration;
      this._time = (new Date()).getTime();
    }
    Object.defineProperties(PolylineTrailLinkMaterialProperty.prototype, {
      isConstant: {
        get: function () {
          return false;
        }
      },
      definitionChanged: {
        get: function () {
          return this._definitionChanged;
        }
      },
      color: Cesium.createPropertyDescriptor('color')
    });
    PolylineTrailLinkMaterialProperty.prototype.equals = function(other) {
      return true;
    }
    PolylineTrailLinkMaterialProperty.prototype.getType = function (time) {
      return 'PolylineTrailImageLink';
    }
    PolylineTrailLinkMaterialProperty.prototype.getValue = function (time, result) {
      if (!Cesium.defined(result)) {
        result = {};
      }
      result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color);
      result.image = Cesium.Material.PolylineTrailLinkImageImage;
      result.time = (((new Date()).getTime() - this._time) % this.duration) / this.duration;
      return result;
    }
    // PolylineTrailLinkMaterialProperty.prototype.equals = function (other) {
    //   return this === other ||
    //     (other instanceof PolylineTrailLinkMaterialProperty &&
    //       Property.equals(this._color, other._color))
    // }
    Cesium.PolylineTrailLinkMaterialProperty = PolylineTrailLinkMaterialProperty;
    Cesium.Material.PolylineTrailLinkImageType = 'PolylineTrailImageLink';
    Cesium.Material.PolylineTrailLinkImageImage = "./static/images/linkPulse.png"; //colors  
    Cesium.Material.PolylineTrailLinkImageSource = "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
                                                      {\n\
                                                           czm_material material = czm_getDefaultMaterial(materialInput);\n\
                                                           vec2 st = materialInput.st;\n\
                                                           vec4 colorImage = texture2D(image, vec2(fract(st.s - time), st.t));\n\
                                                           material.alpha = colorImage.a * color.a;\n\
                                                           material.diffuse = (colorImage.rgb+color.rgb)/2.0;\n\
                                                           return material;\n\
                                                       }";
    Cesium.Material._materialCache.addMaterial(Cesium.Material.PolylineTrailLinkImageType, {
      fabric: {
        type: Cesium.Material.PolylineTrailLinkImageType,
        uniforms: {
          color: new Cesium.Color(1.0, 0.0, 0.0, 1),
          image: Cesium.Material.PolylineTrailLinkImageImage,
          time: 0
        },
        source: Cesium.Material.PolylineTrailLinkImageSource
      },
      translucent: function (material) {
        return true;
      }
    });
  }
  addLine(positions){

    return  this.mapControl.viewer.entities.add({
      polyline: {
          positions: Cesium.Cartesian3.fromDegreesArrayHeights(positions),
          width: 5,
          material: new Cesium.PolylineTrailLinkMaterialProperty(Cesium.Color.DEEPSKYBLUE, 20000)
      }
  });
  }
}